<?php
/**
 * 战绩数据回写
 * 
 */
if ( !defined("SCRIPT_DIR") ) {
	define("SCRIPT_DIR", dirname(__FILE__));
}
$path = explode('/',__FILE__);
if ( !defined("FILE") ) {
	define("FILE",end($path));
}

function path_format($file) {
	return SCRIPT_DIR . '/' . $file;
}


include_once(path_format('lib/logger.php'));
include_once(path_format('config/config.php'));
include_once(path_format('lib/mysql.php'));
include_once(path_format('lib/redis.php'));

class SyncConf {
	public static $sync_sec = 30;//回写数据库时间
	public static $hlen = 20;
	public static $logout_diff = 604800; //86400秒 = 一天， logout_diff时间内无活动删除redis  857019
	public static $begin = 0;
	public static $end = 0;//
}

function sync() {
	$mysql = new MysqlDriver ();
	$rets = $mysql->connect ( Config::$mysql_config );
	if (! $rets) {
		Config::$loggerError->error( FILE, "connect mysql error." );
	}
	
	$redis = new RedisDriver();
	$ret = $redis->initDataRedis(0);
	if (!$ret) {
		Config::$loggerError->error(FILE, "initDataRedis error");
	}
 
	
	/*回写好友战绩数据*/
	
	$customgamedata = $redis->data_redis->keys("rec:friendgame:*");
	$result = "";
    
	/*好友战绩回写数据库-----start--------*/
	if(!empty($customgamedata)){
	    foreach($customgamedata as $val){
		   $arr = explode('_',$val);
		   $pattern = end($arr);
		   $gamedatalogs = $redis->data_redis->hGetAll($val);
		   $result .= "( '".$gamedatalogs['gamelog']."',".$gamedatalogs['playerID'].",".$gamedatalogs['gameID'].",".$gamedatalogs['deskno'].",".$gamedatalogs['score'].",".
		   $gamedatalogs['isOver'].",".$gamedatalogs['boardNumber'].",".$gamedatalogs['deskBuildDate'].",".$gamedatalogs['gameDate'].",".$gamedatalogs['roomid'].",".$gamedatalogs['deskCode'].",".$gamedatalogs['ownerid'].",".
		   (int)$pattern.") ,";	
	    }
	    $resultstr = substr($result,0,strlen($result)-1); 
	    $sql = "insert into customgamelog(gamelog,playerID,gameID,deskno,score,isOver,boardNumber,deskBuildDate,gameDate,roomid,deskCode,ownerid,pattern) values $resultstr";
	    //echo $sql;exit;
	    //Config::$logger->info(FILE,$sql);
	    $mysql->query($sql);
	}
    
	foreach($customgamedata as $val){
			$redis->data_redis->delete($val);
	}
    
    /*----------end------------------*/
	
	/*回写匹配战绩数据*/
    $matchgamedata = $redis->data_redis->keys("rec:matchgame:*");
	if(!empty($matchgamedata)){
	    foreach($matchgamedata as $val){
		    $matchgamedatalogs = $redis->data_redis->hGetAll($val);
			$arr = explode(':',$val);
			$id = $arr[2];
		    $result0 .= "("."'".$id."'".",'".$matchgamedatalogs['gamelog']."',".$matchgamedatalogs['playerID'].",".$matchgamedatalogs['gameID'].",".$matchgamedatalogs['deskno'].",".$matchgamedatalogs['score'].",".
		    $matchgamedatalogs['isOver'].",".$matchgamedatalogs['boardNumber'].",".(int)$matchgamedatalogs['deskBuildDate'].",".(int)$matchgamedatalogs['gameDate'].",".$matchgamedatalogs['roomid'].") ,";	
	    }
	    $resultstr0 = substr($result0,0,strlen($result0)-1); 
	    $sql0 = "insert into matchgamelog(matchID,gamelog,playerID,gameID,deskno,score,isOver,boardNumber,deskBuildDate,gameDate,roomid) values $resultstr0";
	    //echo $sql0;exit;
	    //Config::$logger->info(FILE,$sql);
	    $mysql->query($sql0);
	}
	
	foreach($matchgamedata as $val){
			$redis->data_redis->delete($val);
	}
	
    /*----------end------------------*/
	$redis->deinitDataRedis ();
	$mysql->close ();
}
function main() {
	while(1) {
		sync();	
		echo date('Y:m:d H:i:s')." ZjhSyncDumpCombatGainsData working...\r\n"; 	
		$sync_sec = SyncConf::$sync_sec;
		sleep($sync_sec);
		
	}
}

main ();